<script setup lang="ts">
import { articleHotListApi, pcHomeBannerApi, pcHomeConfigApi } from '~/server/homeApi'
import { useUserStore } from '~/stores/user'
import { linkNavigateTo } from '~/utils/util'
import useOrder from '~/composables/useOrder'
import { useAppStore } from '~/stores/app'
import {reactive, ref, shallowRef} from 'vue'
import { GlobalDatas } from '~/types/user'
import {useNuxtApp} from "nuxt/app"
import { dateFormat } from '~/utils/filter'
const userStore = useUserStore()
const { handleIntoPage } = useOrder()
const activeTab = ref('user')


//获取全局配置数据
const appStore = useAppStore()
const globalData = computed(() => reactive<GlobalDatas>(<GlobalDatas>appStore?.globalData))

//首页banner数据
const { data: bannerList } = await useAsyncData(async () => pcHomeBannerApi(), {
  default: () => [],
})

//资讯热门列表
const { data: articleList } = await useAsyncData(async () => articleHotListApi(), {
  default: () => [],
})

const menuList = ref<Array<string>>([
  {
    name: '宝贝收藏',
    icon: 'icon-baobeishoucang',
    url: '/users/collect_products',
    id: '3',
  },
  {
    name: '关注店铺',
    icon: 'icon-guanzhudianpu',
    url: '/users/collect_merchant',
    id: '4',
  },
  {
    name: '我的足迹',
    icon: 'icon-zuji',
    url: '/users/browsing_history',
    id: '8',
  },
])

/**
 * 登录
 */
const nuxtApp = useNuxtApp()
const userLoginRef = shallowRef()
const handlerLogin = async () => {
  if (!userStore.isLogin) {
    await nuxtApp.$onHandlerLogin()
  }
}

const handlerNuxtLink = async (url: string, type: number) => {
  if (!userStore.isLogin) {
    await nuxtApp.$onHandlerLogin()
  } else {
    await linkNavigateTo(url, { type: type })
  }
}
</script>

<template>
  <div class="bannerIndex h-528px w-100% mb-30px mt-20px">
    <div class="min_wrapper_1200 acea-row">
      <!--分类组件-->
      <classify-card></classify-card>
      <!--轮播-->
      <div class="w-810px ml-225px" style="height: 530px;">
        <el-carousel :interval="5000" height="458px" arrow="always">
          <el-carousel-item v-for="item in bannerList" :key="item" class="w-810px h-458px el-image">
            <a v-if="item.linkUrl" :href="item.linkUrl" target="_blank">
              <el-image :src="item.imageUrl" class="w-810px h-458px el-image b-rd-12px" fit="fill" />
            </a>
            <el-image v-else :src="item.imageUrl" class="w-810px h-458px el-image b-rd-12px" fit="fill" />
          </el-carousel-item>
        </el-carousel>
        <div class="zs-box flex">
            <div class="stat-item">
                <span class="stat-number">12,856.8</span>
                <span class="stat-label">累计交易额(万元)</span>
            </div>
            <div class="stat-item">
                <span class="stat-number">15,680</span>
                <span class="stat-label">注册用户</span>
            </div>
            <div class="stat-item">
                <span class="stat-number">245</span>
                <span class="stat-label">合作基地</span>
            </div>
            <div class="stat-item">
                <span class="stat-number">1,280</span>
                <span class="stat-label">合作农户</span>
            </div>
            <div class="stat-item">
                <span class="stat-number">89</span>
                <span class="stat-label">合作工厂</span>
            </div>
        </div>
      </div>
      <!--登录-->
      <div class="ml-15px w-310px">
        <div class="notice-section">
            <div class="notice-header">
                <div class="title">
                    <div class="icon-wrapper">
                        <font-awesome-icon :icon="['fas', 'fa-bullhorn']" class="icon" />
                        <div class="icon-glow"></div>
                    </div>
                    <span>平台公告</span>
                    
                </div>
                <nuxt-link to="" class="more">
                    更多
                    <font-awesome-icon :icon="['fas', 'chevron-right']" class="arrow-icon" />
                </nuxt-link>
            </div>
            <div class="notice-content">
                <div class="notice-list">
                    <div class="notice-item" @click="handleIntoPage('/activity/information_info', { informationId: item.id })"
            v-for="item in articleList.length === 2 ? articleList : articleList.slice(0, 3)" :key="item.id">
                        <div class="notice-content-wrapper">
                            <div class="notice-badge">
                                <font-awesome-icon :icon="['fas', 'fa-circle']" class="icon" />
                            </div>
                            <div class="notice-text">
                                <span class="notice-title">{{ item.title }}</span>
                                <span class="notice-date">{{ dateFormat(item.createTime) }}</span>
                                
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="user-section h-320px">
            <div class="welcome-text"> Hi~欢迎来到食用菌线上交易平台！ </div>
            <div class="auth-buttons">
                <div class="btn-login cursors btn" @click="handlerLogin">登录</div>
                <div class="btn-register cursors btn" @click="handlerLogin">注册</div>
            </div>
            <div>
                <el-tabs v-model="activeTab" class="user-tabs">
                    <el-tab-pane label="我是买家" name="user">
                        <div class="function-grid">
                            <nuxt-link to="" class="function-item cursor-pointer">
                                <font-awesome-icon :icon="['fas', 'shopping-cart']" class="icon" />
                                <span>订单</span>
                            </nuxt-link>
                            <nuxt-link to="" class="function-item cursor-pointer">
                                <font-awesome-icon :icon="['fas', 'shopping-cart']" class="icon" />
                                <span>订单</span>
                            </nuxt-link>
                            <nuxt-link to="" class="function-item cursor-pointer">
                                <font-awesome-icon :icon="['fas', 'shopping-cart']" class="icon" />
                                <span>订单</span>
                            </nuxt-link>
                            <nuxt-link to="" class="function-item cursor-pointer">
                                <font-awesome-icon :icon="['fas', 'shopping-cart']" class="icon" />
                                <span>订单</span>
                            </nuxt-link>
                            <nuxt-link to="" class="function-item cursor-pointer">
                                <font-awesome-icon :icon="['fas', 'shopping-cart']" class="icon" />
                                <span>订单</span>
                            </nuxt-link>
                            <nuxt-link to="" class="function-item cursor-pointer">
                                <font-awesome-icon :icon="['fas', 'shopping-cart']" class="icon" />
                                <span>订单</span>
                            </nuxt-link>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane label="我是卖家" name="merchant">我是卖家</el-tab-pane>
                    <el-tab-pane label="更多功能" name="more">更多功能</el-tab-pane>
                </el-tabs>
            </div>
        </div>
        <!-- <div class="flex-col-center relative">
          <img
            v-if="!userStore.isLogin"
            @click="handlerLogin"
            class="cursors w-60px h-60px b-rd-50% mb-10px"
            src="@/assets/images/morentou.png"
          />
          <div v-else>
            <img
              :src="userStore.userInfo.avatar"
              @click="linkNavigateTo('/users/user_info', { type: 11 })"
              class="w-60px h-60px b-rd-50% mb-10px cursors"
            />
            <img
              v-show="globalData.userIsPaidMember"
              :src="`${globalData.imageDomain}/crmebimage/presets/viptu.png`"
              class="w-20px h-20px absolute top-40px right-77px"
            />
          </div>
          <div
            v-if="!userStore.isLogin"
            @click="handlerLogin"
            class="cursors mb-20px line1 w-218px text-center text-#666 text-16px"
          >
            Hi~<span class="text-#333">你好</span>
          </div>
          <div v-else class="mb-20px line1 w-218px text-center text-#666 text-16px cursors">
            Hi~<span class="text-#333" @click="linkNavigateTo('/users/user_info', { type: 11 })">{{
              userStore.userInfo.nickname
            }}</span>
          </div>
          <div class="flex-x-center mb-20px">
            <div
              v-if="!userStore.isLogin"
              class="handleBtn w-105px h-32px lh-31px mr-10px cursors"
              @click="handlerLogin"
            >
              登录/注册
            </div>
            <div
              class="handleBtnBorder borderSolE9 w-105px h-32px lh-31px font-color cursors"
              @click="handlerNuxtLink('/merchant/merchant_settled', 0)"
            >
              商户入驻
            </div>
          </div>
          <div class="flex flex-justify-between w-200px">
            <div
              v-for="(item, index) in menuList"
              :key="index"
              class="flex-col-center cursors"
              @click="handlerNuxtLink(item.url, item.id)"
            >
              <div class="iconfont font-color mb-8px" :class="item.icon" style="font-size: 18px"></div>
              <div class="text-12px text-#333">{{ item.name }}</div>
            </div>
          </div>
          <el-divider border-style="dashed" />
        </div>

        <div v-if="articleList && articleList.length">
          <div
            @click="handleIntoPage('/activity/information_info', { informationId: item.id })"
            v-for="item in articleList.length === 2 ? articleList : articleList.slice(0, 3)"
            class="acea-row items-center cursors mb-12px"
          >
            <div class="text-12px font-color bg-#FDEAE9 px-4px py-3px b-rd-2px mr-6px line-heightOne">资讯</div>
            <div class="text-14px text-#666 line1 w-180px fontFamilyOPPOSans">{{ item.title }}</div>
          </div>
        </div> -->
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.bannerIndex {
  :deep(.el-carousel__indicators) {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  :deep(.el-carousel__container) {
    width: 100%;
    height: 100%;
    border-radius: 12px;
    overflow: hidden;
  }
}
.min_wrapper_1200{
    width: 1400px;
    padding: 0 20px;
}
.zs-box{
    backdrop-filter: blur(10px);
    background: #fffffff2;
    border-radius: 6px;
    box-shadow: 0 2px 8px #0000001a;
    display: flex;
    justify-content: space-between;
    padding: 12px 20px;
    margin-top: 10px;
    .stat-item{
        align-items: center;
        display: flex;
        flex: 1;
        flex-direction: column;
        text-align: center;
    }
    .stat-number{
        color: #e4393c;
        font-size: 18px;
        font-weight: 700;
        line-height: 1.2;
        margin-bottom: 4px;
    }
    .stat-label{
        color: #666;
        font-size: 11px;
        line-height: 1.2;
        white-space: nowrap;
    }
}
.notice-section{
    background: #fff;
    border-radius: 6px;
    box-shadow: 0 2px 8px #0000001a;
    height: 195px;
    margin-bottom: 15px;
    padding: 16px;
    .notice-header{
        align-items: center;
        border-bottom: 1px solid #f0f0f0;
        display: flex;
        justify-content: space-between;
        margin-bottom: 12px;
        padding-bottom: 10px;
        .title{
            align-items: center;
            color: #e4393c;
            display: flex;
            font-size: 16px;
            font-weight: 600;
            gap: 10px;
            .icon-wrapper{
                align-items: center;
                background: linear-gradient(135deg, #e4393c, #ff6b6b);
                border-radius: 8px;
                box-shadow: 0 4px 12px #e4393c4d;
                display: flex;
                height: 32px;
                justify-content: center;
                position: relative;
                transition: all .3s ease;
                width: 32px;
                .icon{
                    color: #fff;
                    font-size: 16px;
                    position: relative;
                    z-index: 2;
                }
                .icon-glow{
                    animation: pulse 2s infinite;
                    background: linear-gradient(135deg, #e4393c, #ff6b6b);
                    border-radius: 10px;
                    inset: -2px;
                    opacity: .3;
                    position: absolute;
                    z-index: 1;
                }
            }
            
        }
        .more{
            align-items: center;
            background: #e4393c0d;
            border-radius: 16px;
            color: #999;
            display: flex;
            font-size: 12px;
            gap: 4px;
            padding: 6px 12px;
            text-decoration: none;
            transition: all .3s ease;
            .arrow-icon{
                font-size: 10px;
                transition: transform .3s ease;
            }
            &:hover{
                background: #e4393c1a;
                color: #e4393c;
                transform: translate(2px);
            }
        }
    }
    .notice-content{
        height: 110px;
        overflow: hidden;
        position: relative;
        .notice-list{
             animation: noticeScroll 20s linear infinite;
             &:hover{
                animation-play-state: paused;
             }
            .notice-item{
                align-items: center;
                border-radius: 6px;
                cursor: pointer;
                display: flex;
                justify-content: space-between;
                margin: 2px 0;
                padding: 8px 0;
                position: relative;
                transition: all .3s ease;
                &:hover{
                    background: #e4393c0d;
                    transform: translate(4px);
                }
                .notice-content-wrapper{
                    align-items: center;
                    display: flex;
                    flex: 1;
                    gap: 12px;
                    overflow: hidden;
                    .notice-badge{
                        align-items: center;
                        display: flex;
                        flex-shrink: 0;
                        height: 8px;
                        justify-content: center;
                        width: 8px;
                        .icon{
                            color: #e4393c;
                            font-size: 6px;
                            transition: all .3s ease;
                        }
                    }
                    .notice-text{
                        align-items: center;
                        display: flex;
                        flex: 1;
                        gap: 8px;
                        justify-content: space-between;
                        overflow: hidden;
                        .notice-title{
                            color: #333;
                            flex: 1;
                            font-size: 13px;
                            font-weight: 500;
                            line-height: 1.2;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            transition: color .3s ease;
                            white-space: nowrap;
                            
                        }
                        .notice-date{
                            color: #999;
                            flex-shrink: 0;
                            font-size: 12px;
                            font-weight: 400;
                            white-space: nowrap;
                        }
                    }
                }
            }
        }
    }
}
.user-section{
    background: #fff;
    border-radius: 6px;
    box-shadow: 0 2px 8px #0000001a;
    display: flex;
    flex: 1;
    flex-direction: column;
    padding: 16px;
    .welcome-text{
        color: #333;
        font-size: 14px;
        margin-bottom: 12px;
    }
    .auth-buttons{
        display: flex;
        gap: 10px;
        margin-bottom: 15px;
        .btn{
            border-radius: 4px;
            flex: 1;
            font-size: 14px;
            padding: 8px 0;
            text-align: center;
            text-decoration: none;
            transition: all .3s;
        }
        .btn-login{
            background: #e4393c;
            color: #fff;
        }
        .btn-register{
            background: #fff;
            border: 1px solid #e4393c;
            color: #e4393c;
        }
    }
    .user-tabs{
        margin-bottom: 15px;
        :deep(.el-tabs__nav){
            float: none;
        }
        :deep(.el-tabs__item){
            width:calc(100% / 3);
            padding: 0;
            text-align: center;
            font-size: 13px;
        }
        .function-grid{
            display: grid;
            flex: 1;
            gap: 10px;
            grid-template-columns: repeat(3, 1fr);
            .function-item{
                align-items: center;
                border-radius: 4px;
                color: #666;
                display: flex;
                flex-direction: column;
                justify-content: center;
                padding: 10px 5px;
                text-decoration: none;
                transition: all .3s;
                &:hover{
                    background: #fef5f5;
                    color: #e4393c;
                }
                .icon{
                    font-size: 22px;
                    margin-bottom: 6px;
                }
                span{
                    font-size: 12px;
                }
            }
        }
    }
}
@keyframes pulse {
    0% {
        opacity: .3;
        transform: scale(1)
    }

    50% {
        opacity: .2;
        transform: scale(1.05)
    }

    to {
        opacity: .3;
        transform: scale(1)
    }
}
@keyframes noticeScroll {
    0% {
        transform: translateY(0)
    }

    to {
        transform: translateY(-50%)
    }
}
</style>
